/**
 * 
 */   
package com.cares.core.common.service;  

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.cares.core.base.service.BaseService;
import com.cares.core.common.repository.DicMybatisDao;
import com.cares.entity.common.Dic;

/**   
 * @作者 Stone
 * @创建日期 2014-4-18 上午11:17:57
 * @版本 V1.0
 * @类说明
 */
@Service("dicService")
@Transactional(readOnly = true)
public class DicService extends BaseService {
	
	@Autowired
	private DicMybatisDao dicDao;
	
	/**
	 * 根据ID查询字典信息
	 */
	public Dic get(Long id) {
		return dicDao.get(id);
	}
	
	/**
	 * 查询字典树
	 */
	public List<Dic> getDicList(Dic dic) {
		return dicDao.getDicTree(dic);
	}
	
	/**
	 * 新增或修改菜单
	 * @param menu
	 */
	@Transactional(readOnly = false)
	public void saveOrUpdateDic(Dic dic) {
		if (dic.getDicLevel() == null) {
			dic.setDicLevel(1);
		}
		
		if (dic.getDicLevel() == 1) {
			dic.setUpDicId(new Long(1));
		}

		if (StringUtils.isEmpty(dic.getIconCls())) {
			if (dic.getDicLevel() == 1) {
				//dic.setIconCls("menu-oprateMgr");
			} else {
				//dic.setIconCls("menu-oprateMgr");
			}
			
		}
		if (null != dic.getId()) {
			Dic dbDic = getDic(dic.getId());
			dbDic.setUpDicId(dic.getUpDicId());
			dbDic.setDicName(dic.getDicName());
			dbDic.setDicValue(dic.getDicValue());
			dbDic.setDicValue2(dic.getDicValue2());
			dbDic.setDicValue3(dic.getDicValue3());
			dbDic.setDicLevel(dic.getDicLevel());
			dbDic.setPath(getDicPath(dic.getUpDicId()) + dic.getId() + ">>");
			dbDic.setSort(dic.getSort());
			dbDic.setUsed(dic.getUsed());
			dbDic.setIconCls(dic.getIconCls());
			update(dbDic);
		} else {
			if (null == dic.getUpDicId())
				dic.setUpDicId(new Long(1));
			
			save(dic);
			
			dic.setPath(getDicPath(dic.getUpDicId()) + dic.getId() + ">>");
			update(dic);
		}
	}
	
	/**
	 * 根据上级字典项ID获取可用的字典信息列表
	 * @param nodeId
	 * @return
	 */
	public List getTopDicList(Long nodeId) {
		Dic param = new Dic();
		param.setUpDicId(nodeId);

		List<Dic> dicList = dicDao.getDicTree(param);

		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		for (Dic dic : dicList) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", dic.getId());
			item.put("upDicId", dic.getUpDicId());
			item.put("dicName", dic.getDicName());
			item.put("dicValue", dic.getDicValue());
			item.put("text", dic.getDicName());
			item.put("iconCls", dic.getIconCls());
			item.put("state", "open");
			item.put("optBut", "[ <a href='#' onClick='delDic("
					+ dic.getId() + "\")'>删除</a> ]");
			itemsList.add(item);
		}

		return itemsList;
	}
	
	/**
	 * 根据上级字典项ID获取字典树
	 * @param nodeId 上级字典项ID
	 * @return
	 */
	public List getDicTree(Long nodeId) {
		Dic dic = new Dic();
		dic.setUpDicId(nodeId);

		List<Dic> list = dicDao.getDicTree(dic);
		
		return getAllDicObject(list);
	}
	
	public List getDicList(Long upDicId, String dicCode) {
		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		Map<String, Object> parmMap = new HashMap<String, Object>();
		parmMap.put("upDicId", upDicId);
		parmMap.put("dicCode", dicCode);
		
		List<Dic> dicList = dicDao.find(parmMap);
		
		for (Dic dic : dicList) {
			makeNode(itemsList, dic);
		}
		
		return itemsList;
	}
	
	public void makeNode(List<Map<String, Object>> itemsList, Dic dic) {
		Map<String, Object> item = new HashMap<String, Object>();
		item.put("id", dic.getId());
		item.put("dicName", dic.getDicName());
		item.put("dicValue", dic.getDicValue());
		item.put("dicValue2", dic.getDicValue2());
		item.put("dicValue3", dic.getDicValue3());
		item.put("dicLevel", dic.getDicLevel());

		if (hasSubDic(dic.getId())) {
			item.put("state", "closed");
		} else {
			item.put("state", "open");
		}
		
		itemsList.add(item);
	}
	
	private boolean hasSubDic(Long upDicId) {
		Integer count = dicDao.countByUpDicId(upDicId);
		if (count.intValue() > 0) {
			return true;
		}
		return false;
	}
	
	/**
	 * 根据dicList返回全部字典项(包括子字典项)结果List
	 * @param dicList
	 * @return
	 */
	private List getAllDicObject(List<Dic> dicList) {

		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		for (Dic dic : dicList) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", dic.getId());
			item.put("upDicId", dic.getUpDicId());
			item.put("dicName", dic.getDicName());
			item.put("dicValue", dic.getDicValue());
			item.put("text", dic.getDicName());
			item.put("iconCls", dic.getIconCls());
			item.put("state", "closed");
			item.put("optBut", "[ <a href='#' onClick='delDicBut("
					+ dic.getId() + ",\"DEL\")'>删除</a> ]");
			item.put("children", getSubDicObject(dic.getSubDicList()));
			itemsList.add(item);
		}

		return itemsList;
	}
	
	
	/**
	 * 根据字典项列表subDicList返回结果List
	 * @param menuList
	 * @return
	 */
	private List getSubDicObject(List<Dic> subDicList) {

		List<Map<String, Object>> itemsList = new ArrayList<Map<String, Object>>();
		for (Dic dic : subDicList) {
			Map<String, Object> item = new HashMap<String, Object>();
			item.put("id", dic.getId());
			item.put("upDicId", dic.getUpDicId());
			item.put("dicName", dic.getDicName());
			item.put("dicValue", dic.getDicValue());
			item.put("dicValue2", dic.getDicValue2());
			item.put("dicValue3", dic.getDicValue3());
			item.put("text", dic.getDicName());
			item.put("iconCls", dic.getIconCls());
			item.put("state", "open");
			item.put("optBut", "[ <a href='#' onClick='delDicBut("
					+ dic.getId() + ",\"DEL\")'>删除</a> ]");
			itemsList.add(item);
		}

		return itemsList;
	}
	
	/**
	 * 根据ID查询字典信息
	 */
	public Dic getDic(Long id) {
		Dic dic = get(id);
		Dic upDic = get(dic.getUpDicId());
		if(upDic!=null) {
			dic.setUpDicName(upDic.getDicName());
			dic.setUpDicId(upDic.getId());
		}
		return dic;
	}
	
	/**
	 * 新增字典项信息
	 */
	public void save(Dic dic) {
		dic.setCreateUser(super.getCurrentUserName());
		dic.setCreateTime(dateProvider.getDate());
		dicDao.save(dic);
	}
	
	/**
	 * 更新字典项信息
	 */
	public void update(Dic dic) {
		Assert.notNull(dic.getId(), "缺少主键");
		dic.setUpdateUser(super.getCurrentUserName());
		dic.setUpdateTime(dateProvider.getDate());
		dicDao.update(dic);
	}
	
	/**
	 * 删除字典项信息
	 */
	public void delete(Long id) {
		Dic parm = getDic(id);
		dicDao.deleteDic(parm);
	}
	
	/**
	 * 根据dicId获取字典项路径
	 */
	public String getDicPath(Long dicId) {
		Dic param = dicDao.get(dicId);
		if (null==param) {
			return "1>>";
		}else if (param.getUpDicId().intValue() == 0) {
			return param.getId() + ">>";
		} else {
			return getDicPath(param.getUpDicId()) + param.getId() + ">>";
		}

	}
	
	/**
	 * 根据一级字典value获取字典列表
	 */
	public List<Dic> getDicList4Combox(String value, String mode) {
		List<Dic> dics = dicDao.getDicList4Combox(value);
		
		if("query".equalsIgnoreCase(mode)) {
			Dic all = new Dic();
			all.setId(-1L);
			all.setDicName("全部");
			all.setDicValue("");
			
			dics.add(0, all);
		}
		
		return dics;
	}
	
	/**
	 * 根据一级字典id获取字典列表
	 */
	public List<Dic> getDicListByUpId(Long upDicId) {
		return dicDao.getDicListByUpId(upDicId);
	}
	
	/**
	 * 根据一级字典id获取字典MAP
	 */
	public List<Map> getDicMap(Long upDicId) {
		List<Map> list = new ArrayList<Map>();
		List<Dic> dics = getDicListByUpId(upDicId);
		Map map = null;
		if(dics!=null && dics.size()>0) {
			for (Dic dic : dics) {
				map = new HashMap();
				map.put("PNAME", dic.getDicName());
				map.put("VAL", dic.getDicValue());
				map.put("KEY", dic.getDicValue2());
				map.put("ITEMS", dic.getDicValue3());
				list.add(map);
			}
		}
		return list;
	}
	
	/**
	 * 根据字典的DicValue查找字典列表
	 * @param code
	 * @return
	 */
	public List<Dic> getDicListByCode(String code) {
		return dicDao.getDicListByCode(code);
	}
	
	/**
	 * 根据树的path查找所有叶子
	 * @param path
	 * @return
	 */
	public List<Dic> getLeavesByPath(String path) {
		return dicDao.getLeavesByPath(path);
	}
	
	/**
	 * 根据字典子项ID查找所有父字典名称
	 * @param id
	 * @return
	 */
	public String getParentBusiness(Long id) {
		Dic dic = get(id);
		if(dic!=null) {
			Integer used = dic.getUsed();
			Integer level = dic.getDicLevel();
			if(used!=null && used>0) {
				if(level!=null && level==2) {
					return dic.getDicName();
				} else if(level!=null && level>2) {
					return getParentBusiness(dic.getUpDicId()) + "-" + dic.getDicName();
				}
			}
		}
		return "";
	}
	
	public String getParentBusinessId(Long id) {
		Dic dic = get(id);
		String path = dic.getPath();
		String[] businessIds = path.split(">>");
		if(dic.getDicLevel()==2) {
			return ""+dic.getId();
		} else if(dic.getDicLevel()>2) {
			return businessIds[2];
		}
		return "";
	}
	
	/**
	 * 根据工单业务code查找当前业务类别下一级业务列表
	 * @param code
	 * @return
	 */
	public List<Dic> getFirstBusiness(String code) {
		List<Dic> dics = getDicListByCode(code);
		List<Dic> result = null;
		if(dics!=null && dics.size()>0) {
			Dic parm = dics.get(0);
			result = getDicListByUpId(parm.getId());
		}
		return result;
	}
	
	/**
	 * 根据业务ID查找所有叶子节点
	 * @param id
	 * @return
	 */
	public List<Dic> getLeavesBusiness(Long id) {
		List<Dic> result = null;
		Dic parmDic = get(id);
		if(parmDic!=null && StringUtils.isNotEmpty(parmDic.getPath())) {
			result = getLeavesByPath(parmDic.getPath());
		}
		return result;
	}
}
